package com.course.redisson.service;

import com.course.model.dto.UserDto;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @PROJECT_NAME: course
 * @DESCRIPTION:
 * @Author: 涂玄武
 * @DATE: 2021/5/31 15:16
 */
@Component
public class UserLoginPublisher {

    private static final Logger logger = LoggerFactory.getLogger(UserLoginPublisher.class);

    //构造基于发布-订阅式主题的Key
    private static final String topicKey = "redisson:userLogin:topicKey";

    @Resource
    private RedissonClient redissonClient;

    /**
     * 异步发送消息
     */
    public void sendMsg(UserDto userDto){
        try {
            if (Objects.nonNull(userDto)){
                logger.info("记录用户登录成功后的轨迹-生产者-发送消息：{}",userDto);

                //创建主题
                RTopic topic = redissonClient.getTopic(topicKey);
                //发布消息
                topic.publishAsync(userDto);
            }
        } catch (Exception e) {
            logger.error("记录用户登录成功后的轨迹-生产者-发送消息-发生异常：{}",userDto,e.fillInStackTrace());
        }
    }


}
